package util;

import java.util.ArrayList;
import java.util.List;

import cas.Identity;

import bean.KoordinatBean;
import bean.RuteBean;

public class RuteUtil {
	public static int getUrutanWhereCoordId(List<RuteBean> rute, int coordId){
		for (RuteBean ruteBean : rute) {
			if(ruteBean.getKoordinat().getId() == coordId){
				return ruteBean.getUrutan();
			}
		}
		return -1;
	}
	public static Integer[] getSameCoordIdWhere(Identity myIdentity, Identity anotherTrain, boolean isGetAll){
		List<RuteBean> rute1 = myIdentity.getRute();
		List<RuteBean> rute2 = anotherTrain.getRute();
		List<Integer> result = new ArrayList<Integer>();
		int starter = isGetAll?0:myIdentity.getUrutan();
		for(int i = starter;i<rute1.size();i++){
			KoordinatBean koor = rute1.get(i).getKoordinat();
			starter = isGetAll?0:anotherTrain.getUrutan();
			for(int j = starter;j<rute2.size();j++){
				if(rute2.get(j).getKoordinat().equals(koor)){
					result.add(rute1.get(i).getKoordinat().getId());
				}
			}
		}
		return result.toArray(new Integer[0]);
	}
	public static KoordinatBean getFirstNextWesel(Identity train){
		int i = train.getUrutan()+1;
		KoordinatBean nextWesel1 = null;
		while(i < train.getRute().size()){
			if(train.getRute().get(i).getKoordinat().isWesel()){
				nextWesel1 = train.getRute().get(i).getKoordinat();
				return nextWesel1;
			}
			i++;
		}
		return nextWesel1;
	}
	
	public static Integer[] getSameWeselId(Identity myIdentity,Identity anotherTrain, Integer[] sameId){
		List<RuteBean> rute = anotherTrain.getRute();
		List<Integer> listId = new ArrayList<Integer>();
		KoordinatBean koor1 = myIdentity.getRute().get(myIdentity.getUrutan()).getKoordinat();
		KoordinatBean koor2 = anotherTrain.getRute().get(anotherTrain.getUrutan()).getKoordinat();
		for (int i = 0; i < sameId.length; i++) {
			int j = 0;
			boolean found = false;
			while(!found & j<rute.size()){
				RuteBean bean = rute.get(j);
				if(bean.getKoordinat().getId() == sameId[i]&&
						bean.getKoordinat().isWesel()&&
						koor1.getId()!=sameId[i]&&
						koor2.getId()!=sameId[i]){
					listId.add(bean.getKoordinat().getId());
					found = true;
				}
				j++;
			}
		}
		return listId.toArray(new Integer[0]);
	}
}
